//
//  ActivityView.m
//  MobileOA
//
//  Created by Lawrence Zhang on 12-6-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ActivityView.h"
#import <QuartzCore/QuartzCore.h>

@implementation ActivityView
@synthesize showcanclebtn;
+ (ActivityView *)shareActivityView {
    static ActivityView *_activityManager = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _activityManager = [[self alloc] init];
    });
    
    return _activityManager;
}

- (id)init
{
    self = [super init];
    if (self) 
    {
        isShowing = NO;

        CGSize size = [UIScreen mainScreen].bounds.size;
        CGRect frame = CGRectMake(0, 0, size.width, size.height);
        
        transparent = [[UIView alloc] initWithFrame:frame];
        transparent.backgroundColor = [UIColor clearColor];
        
        bezelView = [[UIView alloc] init];
        bezelView.frame = CGRectMake(0, 0, 180, 70);
        bezelView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.9];
        bezelView.layer.cornerRadius = 4;
        bezelView.layer.masksToBounds = YES;
        bezelView.center = CGPointMake(transparent.frame.size.width / 2, transparent.frame.size.height / 2);
        
        activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activity.center = CGPointMake(bezelView.frame.size.width/2.0 - 50, bezelView.frame.size.height/2);
        
        textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, 140, 30);
        textLabel.textAlignment = NSTextAlignmentLeft;
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:13];
        textLabel.text = @"正在努力加载中...";
        textLabel.textColor = [UIColor whiteColor];
        textLabel.center = CGPointMake(bezelView.frame.size.width / 2+48 , bezelView.frame.size.height / 2);
        
//        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        cancelButton.frame = CGRectMake(160, 15, 20, 20);
//        [cancelButton setImage:[UIImage imageNamed:@"common_cancel_icon.png"] forState:UIControlStateNormal];
//        [cancelButton addTarget:self action:@selector(cancelAllRequest) forControlEvents:UIControlEventTouchUpInside];
        
//        [bezelView addSubview:cancelButton];
        [bezelView addSubview:activity];
        [bezelView addSubview:textLabel];
        
        [transparent addSubview:bezelView];
    }
    return self;
}

- (void)showWithString:(NSString *)title
{
    if (showcanclebtn == 1)
    {
        cancelButton = [[UIButton alloc] init];
        cancelButton.frame = CGRectMake(155, 25, 20, 20);
        [cancelButton setImage:[UIImage imageNamed:@"common_cancel_icon.png"] forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelAllRequest) forControlEvents:UIControlEventTouchUpInside];
        [bezelView addSubview:cancelButton];

    }
    else
    {
        if (cancelButton ) {
            [cancelButton removeFromSuperview];
        }
    }

    UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    [window addSubview:transparent];
    bezelView.alpha=1.0;
    
    [activity startAnimating];
    isShowing = YES;
    
    if (title && [title length] != 0) {
        //不要设置文字了，如果长度太长换行样式不好
        //有的等待提示语句是不一样的，还是启用
        textLabel.text = title;
    }else{
        textLabel.text = @"正在努力加载中...";
    }
    showcanclebtn = 0;
    UIInterfaceOrientation orientation=[UIApplication sharedApplication].statusBarOrientation;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            bezelView.transform = CGAffineTransformMakeRotation(-M_PI /2.0);
            break;
        case UIInterfaceOrientationLandscapeRight:
            bezelView.transform = CGAffineTransformMakeRotation(M_PI /2.0);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            bezelView.transform = CGAffineTransformMakeRotation(M_PI);
            break;
        default:
            bezelView.transform = CGAffineTransformMakeRotation(0.0);
            break;
    }
}

- (void)setText:(NSString *)str
{
    textLabel.text = str;
}

- (void)dismiss
{
    if (!isShowing)
    {
        return;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDidStopSelector:@selector(tRemoveSelf)];
    [UIView setAnimationDelegate:self];
    
    bezelView.alpha = 0.1;
    
    [UIView commitAnimations];
    self.delegate = nil;
}

- (void)tRemoveSelf
{
    isShowing = NO;
    [activity stopAnimating];
    [transparent removeFromSuperview];
    if (cancelButton) {
        [cancelButton removeFromSuperview];
    }

}

- (void)cancelAllRequest
{
//    NSOperationQueue *queue = [[AFAppDotNetAPIClient sharedClient] operationQueue];
//    if (queue && [queue operationCount] != 0) {
//        [queue cancelAllOperations];
//    }
//    //[[NSNotificationCenter defaultCenter] postNotificationName:@"cancelBRequest" object:nil];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(cancelRequest)]) {
//        [self.delegate performSelector:@selector(cancelRequest) withObject:nil];
//    }
//    [self dismiss];
}

@end
